
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsAPI.Hardware
{
    public static class IWin32WindowEx
    {
        public static IntPtr RegisterForUsbEvents(this IWin32Window Window, Guid gClass)
        {
            const int DEVTYP_DEVICEINTERFACE = 0x05;
            const int DEVICE_NOTIFY_WINDOW_HANDLE = 0;

            var hWnd = Window.Handle;

            var oInterfaceIn = new DeviceBroadcastInterface();
            oInterfaceIn.Size = Marshal.SizeOf(oInterfaceIn);
            oInterfaceIn.ClassGuid = gClass;
            oInterfaceIn.DeviceType = DEVTYP_DEVICEINTERFACE;
            oInterfaceIn.Reserved = 0;
            return User32.RegisterDeviceNotification(hWnd, oInterfaceIn, DEVICE_NOTIFY_WINDOW_HANDLE);
        }

        public static bool UnregisterDeviceNotification(this IWin32Window Window) { return User32.UnregisterDeviceNotification(Window.Handle); }
    }
}